
package tests;


import static org.junit.Assert.*;

import java.util.Date;
import java.util.LinkedList;

import junit.framework.TestCase;

import org.junit.*;
import interfaces.*;
import implementation.*;
import tests.mocks.*;


public class TestFindItemImplementation extends TestCase {
	private IFindItem find;
	private IItemCacheMock cacheMock;
	private ISearchSubSupplierMock subSupplierMock;

	
	private void fillCacheAndSubsupplier(IItemCacheMock cacheM, ISearchSubSupplier subSupplierM) {
		find = new IFindItemImpl(cacheM, subSupplierM);
	}
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		cacheMock = new IItemCacheMock();
		subSupplierMock = new ISearchSubSupplierMock();
		//find = new IFindItemImpl(cacheMock, subSupplierMock);
	}
	
	@Test
	public void testNoItemsFound(){
		// Empty cacheMock and subSupplierMock
		fillCacheAndSubsupplier(cacheMock, subSupplierMock);
		assertEquals(find.LookUpItem(0, "").size(), 0);
		
	}
	
	@Test
	public void testNoExpiredItems() {
		LinkedList<IItem> items = new LinkedList<IItem>();
		IItemMock item1 = new IItemMock();
		item1.setHit(new Date());
		IItemMock item2 = new IItemMock();
		item2.setHit(new Date());
		items.add(item1);
		items.add(item2);
		cacheMock.AddItems(items);
		fillCacheAndSubsupplier(cacheMock, subSupplierMock);
		find.SetCacheExpireTime(2);
		assertEquals(find.LookUpItem(0, "").size(),2);
	}
	
	@Test
	public void testExpiredItems(){
		LinkedList<IItem> items = new LinkedList<IItem>();
		IItemMock item1 = new IItemMock();
		Date date1 = new Date((new Date().getTime())-5*1000*3600);
		item1.setHit(date1);
		IItemMock item2 = new IItemMock();
		item2.setHit(new Date());
		items.add(item1);
		items.add(item2);
		cacheMock.AddItems(items);
		fillCacheAndSubsupplier(cacheMock, subSupplierMock);
		find.SetCacheExpireTime(2);
		assertEquals(find.LookUpItem(0, "").size(),1);
	}
	
	@Test
	public void testSubSupplierGotStock(){
		LinkedList<IItem> items = new LinkedList<IItem>();
		IItemMock item1 = new IItemMock();
		Date date1 = new Date((new Date().getTime())-5*1000*3600);
		item1.setHit(date1);
		items.add(item1);
		subSupplierMock.addItems(items);
		fillCacheAndSubsupplier(cacheMock, subSupplierMock);
		find.SetCacheExpireTime(2);
		assertEquals(find.LookUpItem(0, "").size(),1);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	
}

